package com.blockchain.console.servlets.financial.cz;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.blockchain.console.service.financial.RechargeQrCodeManager;
import com.blockchain.console.service.financial.entity.RechargeQrCode;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.http.upload.PartFile;
import com.blockchain.framework.http.upload.UploadFile;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;

@MultipartConfig
@Right(id = "C_FINANCIAL_QR", name = "充值二维码管理")
public class Qrgl extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {

		RechargeQrCodeManager rechargeQrManager = serviceSession
				.getService(RechargeQrCodeManager.class);
		request.setAttribute("rechargeQr", rechargeQrManager.search());
		forwardView(request, response, getClass());
	}

	@Override
	protected void processPost(final HttpServletRequest request, final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		
		try {
			
			RechargeQrCodeManager rechargeQrManager = serviceSession
					.getService(RechargeQrCodeManager.class);
			rechargeQrManager.edit(new RechargeQrCode() {
				
				@Override
				public UploadFile getFirstImage() throws Throwable {

					Part part = request.getPart("imagefir");
					if (part == null || part.getContentType() == null
							|| part.getSize() == 0) {
						return null;
					}
					return new PartFile(part);
				}

				@Override
				public UploadFile getSecondImage() throws Throwable {

					Part part = request.getPart("imagesec");
					if (part == null || part.getContentType() == null
							|| part.getSize() == 0) {
						return null;
					}
					return new PartFile(part);
				}

				@Override
				public UploadFile getThirdImage() throws Throwable {

					Part part = request.getPart("imagethir");
					if (part == null || part.getContentType() == null
							|| part.getSize() == 0) {
						return null;
					}
					return new PartFile(part);
				}

				@Override
				public UploadFile getFourthImage() throws Throwable {

					Part part = request.getPart("imagefour");
					if (part == null || part.getContentType() == null
							|| part.getSize() == 0) {
						return null;
					}
					return new PartFile(part);
				}
			});
			sendRedirect(request, response,
					getController().getURI(request, getClass()));
		} catch (ParameterException | LogicalException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			processGet(request, response, serviceSession);
		}
	}

}
